/*
 * bw_Sqlite.h
 *
 *  Created on: 2018年9月5日
 *  Author: Degang.Wu (windragon@126.com)
 *  Desc:	bw sqlite3 wrap v0.1
 */

#ifndef BW_SQLITE_H_
#define BW_SQLITE_H_

#include "bw_public.h"
#include "sqlite3.h"

namespace bwcgi
{

typedef std::map<std::string, std::string> BWRow;
typedef std::vector<BWRow> BWRows;

class BWCursor
{
public:
	BWCursor(BWRow& row);
	virtual ~BWCursor();

	int	getInt(int idx);
	int getInt(const char* col);

	S64 getInt64(int idx);
	S64 getInt64(const char* col);

	std::string getString(int idx);
	std::string getString(const char* col);

private:
	std::vector<std::string> cursor;
	BWRow 	row;
};
class BWSqlite
{
public:
	BWSqlite(const char* dbfile);
	virtual ~BWSqlite();

	//返回多行记录
	BWRows QeuryRows(const char* sql);

	//限制返回一行记录
	BWRow QueryRow(const char* sql);

	// 返回插入数据的id
	int Inserted_id(const char* table_name);

	int Insert(const char* sql);
	int Delete(const char* sql);
	int Update(const char* sql);

	//判断指定表是否存在
	static bool ExistTable(const char* dbfile, const char* tb);
	static bool ExistTable(BWSqlite* pDb, const char* tb);

	int Exec(const char* sql);

protected:

private:
	std::string sDbfile;
	sqlite3 *pDb;
};

} /* namespace bwcgi */

#endif /* BW_SQLITE_H_ */
